// File: src/game/xp/XPSystem.h
#pragma once
#include <atomic>
#include <iostream>
#include <vector>
#include "game/territory/Territory.h"

class XPSystem {
public:
    void addXP(int amount) {
        currentXP += amount;
        checkLevelUp();
    }
    
    void setXP(int xp) {currentXP = xp;}

    void saveToFile(const std::string& username,
        const std::vector<Territory>& territories);  // 保存XP值和领土数据到文件
    void loadFromFile(const std::string& username,
        std::vector<Territory>& territories); // 从文件加载XP值和领土数据

    int getCurrentXP() const { return currentXP; }
    int getCurrentLevel() const { return currentLevel; }
    
    int getXPToNextLevel() const {
        if (currentLevel >= sizeof(levelThresholds)/sizeof(levelThresholds[0])) 
            return 0;
        return levelThresholds[currentLevel] - currentXP;
    }
    
private:
    void checkLevelUp() {
        while (currentLevel < sizeof(levelThresholds)/sizeof(levelThresholds[0]) && 
               currentXP >= levelThresholds[currentLevel]) {
            currentLevel++;
            std::cout << "\033[1;32m恭喜升级! 当前等级: " << currentLevel << "\033[0m" << std::endl;
        }
    }
    
    std::atomic<int> currentXP{0};
    std::atomic<int> currentLevel{1};
    const int levelThresholds[10] = {100, 300, 600, 1000, 1500, 2100, 2800, 3600, 4500, 5500};
};
